package com.wangtao.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BIOServer {

    public static void main(String[] args) throws IOException {
        //创建一个线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        //创建ServerSocket
        ServerSocket serverSocket = new ServerSocket(6666);

        System.out.println("服务器启动了");

        while (true) {
            System.out.println("==线程id==="+Thread.currentThread().getId()+"==线程名称=="+ Thread.currentThread().getName());
            //监听等待客户端连接
            System.out.println("等待连接...");
            final Socket socket = serverSocket.accept();
            System.out.println("连接到了一个客户端");

            executorService.execute(()->{
                handler(socket);
            });

        }
    }

    public static void handler(Socket socket) {
        try {
            System.out.println("线程id==="+Thread.currentThread().getId()+"==线程名称=="+ Thread.currentThread().getName());
            byte[] bytes = new byte[1024];
            //通过socket获取输入流
            InputStream inputStream = socket.getInputStream();
            int len = 0;
            System.out.println("read...");
            while ((len=inputStream.read(bytes))!=-1) {
                System.out.println("==线程id==="+Thread.currentThread().getId()+"==线程名称=="+ Thread.currentThread().getName());
                System.out.println(new String(bytes, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.out.println("关闭和client的连接");
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
